
package com.putao.album.popupwindow;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.putao.album.R;
import com.putao.album.share.ShareTools;
import com.putao.album.util.CommonUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by yanglun on 15/5/8.
 */
public class PtThirdSharePopupWindow implements View.OnClickListener {
    private Context mContext;
    private Activity mActivity;
    private static PopupWindow popupWindow = null;
    private static PtThirdSharePopupWindow inst;
    private PopupClickListener mPopupClickListener;
    private LinearLayout share_wechat_ll, share_wechat_friend_ll, share_qq_zone_ll, share_weibo_ll;
    public static ShareTools mShareTools;
    public String mPhotoUrl;
    public String mWebUrl;
    public String mAge;
    public static int SHARE_WEB = 0, SHARE_PHOTO = 1;
    private int curShareType;

    public static PopupWindow Create(Activity activity, String photoUrl, String age, int type) {
        if (inst == null) {
            inst = new PtThirdSharePopupWindow();
            inst.mContext = activity;
            inst.mActivity = activity;
            inst.mPhotoUrl = photoUrl;
            inst.curShareType = type;
            inst.mAge = age;
            inst.initPopuptWindow();
        }
        //        inst.mPopupClickListener = mPopupClickListener;
        return popupWindow;
    }

    public static PopupWindow Create(Activity activity, String webUrl, String photoUrl, String age, int type) {
//        if (inst == null) {
        inst = new PtThirdSharePopupWindow();
        inst.mContext = activity;
        inst.mActivity = activity;
        inst.mPhotoUrl = photoUrl;
        inst.mWebUrl = webUrl;
        inst.curShareType = type;
        inst.mAge = age;
        inst.initPopuptWindow();
//        } else {
//            inst.reInitPopupWindow();
//        }
        return popupWindow;
    }

    protected void reInitPopupWindow() {
        mShareTools.initWeiboShareApi();
    }

    protected void initPopuptWindow() {
        View popupWindow_view = LayoutInflater.from(mContext).inflate(R.layout.layout_home_page_third_share, null, false);
        RelativeLayout root_rl = (RelativeLayout) popupWindow_view.findViewById(R.id.root_rl);
        share_wechat_ll = (LinearLayout) popupWindow_view.findViewById(R.id.share_wechat_ll);
        share_wechat_friend_ll = (LinearLayout) popupWindow_view.findViewById(R.id.share_wechat_friend_ll);
        share_qq_zone_ll = (LinearLayout) popupWindow_view.findViewById(R.id.share_qq_zone_ll);
        share_weibo_ll = (LinearLayout) popupWindow_view.findViewById(R.id.share_weibo_ll);
        popupWindow = new PopupWindow(popupWindow_view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);//??PopupWindow??
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        root_rl.setOnClickListener(this);
        share_wechat_ll.setOnClickListener(this);
        share_wechat_friend_ll.setOnClickListener(this);
        share_qq_zone_ll.setOnClickListener(this);
        share_weibo_ll.setOnClickListener(this);
        if (curShareType == SHARE_PHOTO) {
            mShareTools = new ShareTools(mActivity, mPhotoUrl, mAge);
        } else if (curShareType == SHARE_WEB) {
            mShareTools = new ShareTools(mActivity, mPhotoUrl, mWebUrl, mAge);
        }
        root_rl.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK)
                    popupWindow.dismiss();
                return false;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.share_wechat_ll:
                if (CommonUtils.isAppInstalled(mContext, "com.tencent.mm")) {
                    if (curShareType == SHARE_WEB) {
                        mShareTools.sendWebToWeixin(true);
                    } else {
                        mShareTools.sendBitmapToWeixin(true);
                    }

                } else {
                    Toast.makeText(mContext, "未安装微信", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.share_wechat_friend_ll:

                if (CommonUtils.isAppInstalled(mContext, "com.tencent.mm")) {
                    if (curShareType == SHARE_WEB) {

                        mShareTools.sendWebToWeixin(false);

                    } else {
                        mShareTools.sendBitmapToWeixin(false);
                    }

                } else {
                    Toast.makeText(mContext, "未安装微信", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.share_qq_zone_ll:
                if (CommonUtils.isAppInstalled(mContext, "com.tencent.mobileqq")) {
                    if (curShareType == SHARE_WEB) {
                        mShareTools.doShareWebToQzone();
                    } else {
                        mShareTools.doShareToQzone();
                    }

                } else {
                    Toast.makeText(mContext, "未安装QQ", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.share_weibo_ll:
                if (CommonUtils.isAppInstalled(mContext, "com.sina.weibo")) {
                    if (curShareType == SHARE_WEB) {
                        mShareTools.doShareWebToWeibo();
                    } else {
                        mShareTools.doShareToWeibo();
                    }

                } else {
                    Toast.makeText(mContext, "未安装新浪微博", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.root_rl:
                popupWindow.dismiss();
                break;
        }
    }

    public interface PopupClickListener {
        public String getInviateValue();
    }

}
